function httpRequest(url, type) {
	'use strict';
	var promise = new Promise(function (resolve, reject) {
		var client = new XMLHttpRequest();
		client.open("GET", url);
		client.onreadystatechange = function () {
			if (this.readyState === this.DONE) {
				if (this.status === 200) {
					resolve(this.response);
				} else {
					reject(this);
				}
			}
		};
		client.responseType = type.toLowerCase();
		client.send();
	});
	return promise;
}

function parseYodaoXml(text) {
	'use strict';
	var xmlDoc = (new DOMParser()).parseFromString(text, 'application/xml'),
		fanyi = xmlDoc.querySelector('fanyi-result'),
		translations = xmlDoc.querySelectorAll('custom-translation>translation>content');
	
	if(fanyi){
		var tran = xmlDoc.querySelector('tran').textContent;
		returnStr = '<div class="yourDictFanyi">' + tran + '</div>'
		return {parsedHTML: returnStr, title: '翻译', type: 'fanyi'};
		
	}else if(translations){
		var yodaoWebDict = xmlDoc.querySelector('yodao-web-dict'),
			title = xmlDoc.querySelector('return-phrase').textContent,
			temTranslationLen = translations.length,
			phonetic = {},
			speech = {},
			returnStr = '',
			hasSpeech = false;
	
		speech.us = xmlDoc.querySelector('us-speech');
		speech.uk = xmlDoc.querySelector('uk-speech');
		if(speech.us || speech.uk){
			hasSpeech = true;
			speech.us = encodeURI(speech.us.textContent);
			speech.uk = encodeURI(speech.uk.textContent);
		}
		
		phonetic.us = xmlDoc.querySelector('uk-phonetic-symbol');
		phonetic.uk = xmlDoc.querySelector('us-phonetic-symbol');
		if (phonetic.us || phonetic.uk) {
			returnStr += '<ul class="yourDictPhonetic">';
			if (phonetic.us) {
				phonetic.us = phonetic.us.textContent;
				returnStr += '<li>[美] [' + phonetic.us + ']<a class="yourDictSpeechPlayUs"></a></li>';
			}
			if (phonetic.uk) {
				phonetic.uk = phonetic.uk.textContent;
				returnStr += '<li>[英] [' + phonetic.uk + ']<a class="yourDictSpeechPlayUk"></a></li>';
			}
			returnStr += '</ul>';
		} else if (hasSpeech) {
			returnStr += '<ul class="yourDictPhonetic">' +
			'<li>[美]<a class="yourDictSpeechPlayUs"></a></li>' +
			'<li>[英]<a class="yourDictSpeechPlayUk"></a></li>';
			returnStr += '</ul>';
		}
		
		returnStr += '<ul class="yourDictTranslates">';
		for (var i = 0; i < temTranslationLen; i++) {
			returnStr += '<li>' + translations[i].textContent.trim() + '</li>';
		}
		returnStr += '</ul>';

		if(yodaoWebDict){
			var yodaoWebDicts = yodaoWebDict.children;

			returnStr += '<ul class="yourDictWebDicts">';
			for (var i = 0, temyodaoWebDictLen = yodaoWebDicts.length; i < temyodaoWebDictLen; i++) {
				var temWebDictKey = yodaoWebDicts[i].querySelector('key').textContent.trim(),
					temWebDictValues = yodaoWebDicts[i].querySelectorAll('value'),
					temWebDictValueLen = temWebDictValues.length,
					temWebDictValueArr = [];
				for (var j = 0; j < temWebDictValueLen; j++) {
					temWebDictValueArr.push(temWebDictValues[j].textContent.trim());
				}
				returnStr += '<li>' + temWebDictKey + ' : ' + temWebDictValueArr.join(' | ') + '</li>';
			}
			returnStr += '</ul>';
		}
		return {parsedHTML: returnStr, title: title, speech: speech, type: 'word', hasSpeech: hasSpeech};
	}else{
		return {parsedHTML: '查询失败', title: '', type: 'word', hasSpeech: false};
	}
}


/*点击播放图标时获取读音并播放*/
function getYodaoSpeech(parsedObj, yourDictWrapper) {
	if (parsedObj.hasSpeech) {
		var speechStorage = {};
		['Us', 'Uk'].forEach(function (speechType) {
			var existSpeech = yourDictWrapper.querySelector('.yourDictSpeechPlay' + speechType);
			if (existSpeech) {
				existSpeech.addEventListener('click', function () {
					if (speechStorage[speechType]) {
						speechStorage[speechType].play();
					} else {
						httpRequest('https://dict.youdao.com/dictvoice?audio=' + parsedObj.speech[speechType.toLowerCase()], 'blob')
						.then(function (blob) {
							speechStorage[speechType] = document.createElement('audio');
							speechStorage[speechType].src = URL.createObjectURL(blob);
							speechStorage[speechType].type = "audio/mpeg";
							speechStorage[speechType].play();
						});
					}
				});
			}
		});
	}
}

function initStorage(callback){
	window.getTextMethod = {
		point: false,
		stroke: true,
		dblclick: true
	}
	chrome.storage.local.get('getTextMethod', function (result) {
		if (result.getTextMethod) {
			window.getTextMethod = result.getTextMethod;
		} else {
			chrome.storage.local.set({'getTextMethod': window.getTextMethod});
		}
		if(callback) callback();
	});
}


function animate(element, target, timeLen, callback) {
	var timer = null,
		timeLen = timeLen || 300,
		perLoopTime = 30,
		speeds = {},
		loopNum = timeLen / perLoopTime,
		loopNumNow = 0;
	for (var key in target) {
		speeds[key] = (parseFloat(target[key]) - parseFloat(getComputedStyle(element)[key])) / loopNum;
	}
	clearInterval(timer);
	timer = setInterval(function () {
		for (var key in target) {
			if (key === 'opacity') {
				element.style.opacity = parseFloat(getComputedStyle(element)['opacity']) + speeds.opacity;
			} else {
				element.style[key] = parseFloat(getComputedStyle(element)[key]) + parseFloat(speeds[key]) + 'px';
			}
		}
		loopNumNow++;
		if(loopNumNow >= loopNum){
			for (var key in target) {
				element.style[key] = target[key];
			}
			clearInterval(timer);
			if (callback) callback();
		}
	}, perLoopTime);
}